package online.flowerinsnow.md5;

import online.flowerinsnow.md5.util.MD5Utils;
import java.io.File;

public class MD5 {
    public static void main(String[] args) {
        if (args.length > 0) {
            StringBuilder sb = new StringBuilder();
            for (int i = 1; i < args.length; i++) {
                sb.append(args[i]);
                if (i + 1 < args.length) sb.append(" ");
            }

            switch (args[0]) {
                case "-f":
                    File file = new File(sb.toString());
                    String var1 = MD5Utils.md5(file);
                    if (var1 == null) {
                        printError();
                    } else System.out.println(var1);
                    break;
                case "-s":
                    String var2 = MD5Utils.md5(sb.toString());
                    if (var2 == null) {
                        printError();
                    } else System.out.println(var2);
                    break;
                default:
                    printUsage();
            }
        } else printUsage();
    }

    private static void printUsage() {
        System.out.println("用法: md5 <类型> <数值>\n" +
                "\t类型：\n" +
                "\t\t-f：文件\n" +
                "\t\t-s: 字符串");
    }

    private static void printError() {
        System.err.println("MD5加密失败");
    }
}
